//-----------------------------------------------------------------------------
// Bend2D - Physics Engine
// Roberto Bender <beto.bender@gmail.com>
// Description: 
// Date: 10/03/2010
//-----------------------------------------------------------------------------
#include "durable.h"
#include <cstdlib>
using namespace bend2d;

Durable::Durable() :
    m_Resetter(NULL),
    m_IterationsToLive(0),
    m_IterationsLiving(0),
    m_TimeToLive(0),
    m_TimeLiving(0),
    m_IterationSensitive(false),
    m_TimeSensitive(false)
{
}

void Durable::SetIterationSense(bool sensitive, unsigned int iterationsToLive)
{
    m_IterationSensitive = sensitive;
    m_IterationsToLive = iterationsToLive;
    Reset();
}

void Durable::SetTimeSense(bool sensitive, double timeToLive)
{
    m_TimeSensitive = sensitive;
    m_TimeToLive = timeToLive;
    Reset();
}

bool Durable::IsIterationSensitive() const
{
    return m_IterationSensitive;
}

bool Durable::IsTimeSensitive() const
{
    return m_TimeSensitive;
}

unsigned int Durable::GetIterationsToLive() const
{
    return m_IterationsToLive;
}

double Durable::GetTimeToLive() const
{
    return m_TimeToLive;
}

void Durable::Reset()
{
    m_TimeLiving = 0;
    m_IterationsLiving = 0;
}

bool Durable::Live(double timeSinceLastFrame, unsigned int iterations)
{
    bool ret = false;
    if(m_IterationSensitive)
    {
        m_IterationsLiving += iterations;
        ret = m_IterationsLiving >= m_IterationsToLive;
    }
    
    if(m_TimeSensitive)
    {
        m_TimeLiving += timeSinceLastFrame;
        ret = m_TimeLiving >= m_TimeToLive;
    }

    return !ret;
}

void Durable::SetDurableResetter(DurableResetter * ptr)
{
    m_Resetter = ptr;
}

DurableResetter * Durable::GetDurableResetter() const
{
    return m_Resetter;
}

Durable::DurableList::iterator& Durable::GetDurableIterator()
{
    return m_Iterator;
}
